'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class WindowStateToVisibilityConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim x As WindowState = DirectCast(value, WindowState)

        If DirectCast(parameter, String) = "restore" Then
            If x = WindowState.Maximized Then
                Return Visibility.Visible
            Else
                Return Visibility.Collapsed
            End If
        Else
            If x = WindowState.Normal Then
                Return Visibility.Visible
            Else
                Return Visibility.Collapsed
            End If
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function
End Class

Public Class WindowTheme
    Private Sub OnMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If System.Windows.Input.Mouse.LeftButton = MouseButtonState.Pressed Then wnd.DragMove()
    End Sub

    Private Sub OnWindowClose(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim wnd As Window

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then wnd.Close()
    End Sub

    Private Sub OnWindowRestore(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim wnd As Window

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then wnd.WindowState = WindowState.Normal
    End Sub

    Private Sub OnWindowMaximize(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim wnd As Window

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then wnd.WindowState = WindowState.Maximized
    End Sub

    Private Sub OnWindowMinimize(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim wnd As Window

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then wnd.WindowState = WindowState.Minimized
    End Sub


    Private Sub OnMouseDownOnGrid(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window

        If Input.Mouse.LeftButton = Input.MouseButtonState.Pressed Then
            wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

            If wnd IsNot Nothing Then wnd.DragMove()
        End If
    End Sub

    Private Sub OnSizeNorth(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.North)
        End If
    End Sub

    Private Sub OnSizeSouth(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.South)
        End If
    End Sub

    Private Sub OnSizeEast(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.East)
        End If
    End Sub

    Private Sub OnSizeWest(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.West)
        End If
    End Sub

    Private Sub OnSizeNorthEast(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.NorthEast)
        End If
    End Sub

    Private Sub OnSizeNorthWest(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.NorthWest)
        End If
    End Sub

    Private Sub OnSizeSouthEast(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.SouthEast)
        End If
    End Sub

    Private Sub OnSizeSouthWest(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim wnd As Window
        Dim wse As JCsTools.Wpf.Extenders.WindowResizeExtender

        wnd = TryCast(DirectCast(sender, FrameworkElement).TemplatedParent, Window)

        If wnd IsNot Nothing Then
            wse = JCsTools.Wpf.Extenders.WindowExtenderProvider.GetResizeExtender(wnd)

            wse.DragSize(JCsTools.Wpf.Extenders.SizingAction.SouthWest)
        End If
    End Sub
End Class
